package com.situ.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.mapper.PermissionMapper;
import com.situ.mall.pojo.Permission;
import com.situ.mall.pojo.query.PermissionQuery;
import com.situ.mall.service.IPermissionService;
import com.situ.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2025-06-26
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageResult list(PermissionQuery permissionQuery) {
        // select * from permission where name='zhang' and create_time between xxx and xxx limit 0,10;
        IPage<Permission> page = new Page<>(permissionQuery.getPage(), permissionQuery.getLimit());
        //查询条件
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(permissionQuery.getName()), "name", permissionQuery.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(permissionQuery.getPath()), "path", permissionQuery.getPath());
        queryWrapper.eq(!ObjectUtils.isEmpty(permissionQuery.getRole()), "role", permissionQuery.getRole());
        queryWrapper.between(!ObjectUtils.isEmpty(permissionQuery.getBeginCreateTime()) && !ObjectUtils.isEmpty(permissionQuery.getEndCreateTime()),
                "create_time", permissionQuery.getBeginCreateTime(), permissionQuery.getEndCreateTime() );
        permissionMapper.selectPage(page, queryWrapper);

        return new PageResult(0, "", (int)page.getTotal(), page.getRecords());
    }

    @Override
    public List<Permission> selectByRole(Integer role) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", role);
        List<Permission> list = permissionMapper.selectList(queryWrapper);
        return list;
    }
}
